package com.Brave.manager.Controller;

import com.Brave.manager.Daomian.Student;
import com.Brave.manager.Service.StudentService;

import java.util.Scanner;

public class StudentController {
    StudentService StudentService = new StudentService();
    Scanner S = new Scanner(System.in);

    public void start() {

        StudentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String select = S.next();
            switch (select) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    updateStudent();
                    break;
                case "4":
                    showStudent();
                    break;
                case "5":
                    System.out.println("退出");
                    break StudentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入：");
                    break;
            }
        }

    }

    private void updateStudent() {
        String id = inputStudentId();
        Student stu  = inputStudentInfo(id);
        StudentService.updataStudent(id,stu);
        System.out.println("修改成功！");
    }

    private void deleteStudent() {
        String id = inputStudentId();
        StudentService.deleteStudent(id);
        System.out.println("删除成功！");
    }

    private String inputStudentId() {
        String id;
        while (true) {
            System.out.println("请输入学生学号：");
            id = S.next();
            boolean res = StudentService.isExists(id);
            if (!res) {
                System.out.println("您输入的学号不存在，请重新输入：");
            } else {
                break;
            }
        }
        return  id;
    }

    private void showStudent() {
        Student[] stus = StudentService.findAllStudent();
        if (stus == null) {
            System.out.println("查无信息，请添加后查询！");
            return;
        }
        System.out.println("学号" + "\t\t" + "姓名" + "\t\t" + "年龄" + "\t\t" + "性别");
        for (int i = 0; i < stus.length; i++) {
            Student stu = stus[i];
            if (stu != null) {
                System.out.println(stus[i].getId() + "\t\t" + stus[i].getName() + "\t\t" + stus[i].getAge() + "\t\t" + stus[i].getSex());
            }
        }
    }

    private void addStudent() {
        String id;
        while (true) {
            System.out.println("请输入学生学号：");
            id = S.next();
            boolean res = StudentService.isExists(id);
            if (res) {
                System.out.println("您输入的学号已存在，请重新输入：");
            } else {
                break;
            }
        }
        Student stu = inputStudentInfo(id);
        boolean result = StudentService.addStudent(id, stu);
        if (result) {
            System.out.println("添加成功！");
        } else {
            System.out.println("添加失败！");
        }
    }

    private Student inputStudentInfo(String id) {
        System.out.println("请输入学生姓名：");
        String name = S.next();
        System.out.println("请输入学生年龄：");
        String age = S.next();
        System.out.println("请输入学生性别：");
        String sex = S.next();
        Student stu = new Student(id, name, age, sex);
        return stu;
    }
}
